package com.zaofan.utils;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.io.IOException;
import java.math.BigDecimal;

public class SendSms {

	private static String Url = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";

	public static int sendRegisterCode(String mobile, String num) {
		String content = new String("您的验证码是：【" + num + "】。请不要把验证码泄露给其他人。如非本人操作，可不用理会！");
		return sendSms(mobile, content);
	}

	public static int sendFindCode(String mobile, String num) {
		String content = new String("您的验证码是：【" + num + "】。修改密码后请妥善保管好您的密码！");
		return sendSms(mobile, content);
	}

	public static int sendNewPass(String mobile, String pass) {
		String content = new String("您的新密码是：【" + pass + "】，请不要把密码泄露给其他人，如非本人操作，可不用理会！");
		return sendSms(mobile, content);
	}

	public static int sendRecharge(String mobile, BigDecimal money, BigDecimal sendMoney) {
		String content = new String(
				"您充值的【" + money + "】元已成功，您已获得乐吃购赠送的【" + sendMoney + "】元早餐抵用金，可在您的用户中心查看，乐吃购祝您用餐愉快！");
		return sendSms(mobile, content);
	}

	public static int sendRegister(String mobile) {
		String content = new String("您的注册已生效，恭喜【" + mobile + "】获得10元乐吃购早餐抵用金，可在您的用户中心查看，乐吃购祝您用餐愉快！");
		return sendSms(mobile, content);
	}

	public static int sendSuccessOrder(String mobile, String orderNo) {
		String content = new String(
				"您的订单【" + orderNo + "】已经生效,吃早餐，要有范！早范君将会在明日上午将您的美味早餐配送至您的指定地点！如需帮助请联系客服:4000-520-816");
		return sendSms(mobile, content);
	}

	public static int sendSms(String mobile, String content) {
		HttpClient client = new HttpClient();
		PostMethod method = new PostMethod(Url);
		client.getParams().setContentCharset("UTF-8");
		method.setRequestHeader("ContentType", "application/x-www-form-urlencoded;charset=UTF-8");
		// 提交短信
		NameValuePair[] data = { new NameValuePair("account", "cf_52letsgo"), new NameValuePair("password", "678454E281818A13FC23D73B98C4347B"), // 密码可以使用明文密码或使用32位MD5加密
				new NameValuePair("mobile", mobile), new NameValuePair("content", content), };
		method.setRequestBody(data);
		try {
			client.executeMethod(method);
			String SubmitResult = method.getResponseBodyAsString();
			Document doc = DocumentHelper.parseText(SubmitResult);
			Element root = doc.getRootElement();
			String code = root.elementText("code");
			String msg = root.elementText("msg");
			String smsid = root.elementText("smsid");
			System.out.println(code);
			System.out.println(msg);
			System.out.println(smsid);
			return Integer.valueOf(code);
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		return 0;
	}

	public static void main(String[] args) {
		int result = sendRegisterCode("18649739717", "4562");
		System.err.println(result);
	}

    public static int sendQuickCode(String mobile, String code) {
    	String content = new String("您的验证码是：【" + code + "】。请不要把验证码泄露给其他人。如非本人操作，可不用理会！");
		return sendSms(mobile, content);
    }
}